#include <isa.h>
#include <cpu/cpu.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "sdb.h"
#include <memory/paddr.h>
//#include "watchpoint.c"


typedef struct watchpoint {
  int NO;
  struct watchpoint *next;
  char exp[32];
  word_t value;

  /* TODO: Add more members if necessary */

} WP;
void print_wp();
WP* new_wp(char * exp);
int free_wp(WP *wp);
int delete_wp( int num);


static int is_batch_mode = false;

void init_regex();
void init_wp_pool();

/* We use the `readline' library to provide more flexibility to read from stdin. */
static char* rl_gets() {
  static char *line_read = NULL;

  if (line_read) {
    free(line_read);
    line_read = NULL;
  }

  line_read = readline("(nemu) ");

  if (line_read && *line_read) {
    add_history(line_read);
  }

  return line_read;
}

static int cmd_c(char *args) {
  cpu_exec(-1);
  return 0;
}


static int cmd_q(char *args) {
  set_nemu_state(NEMU_QUIT, 0x80000000, 0);
  return -1;
}

static int cmd_help(char *args);


static int cmd_si(char *args){
  int step = 1;
  if (args != NULL)
  sscanf(args,"%d",&step);
  cpu_exec(step);
  return 0;
}

//读内存简单版本
static int cmd_x(char *args){
  int times;//循环次数
  paddr_t address;//读取地址
  sscanf(args,"%d %x", &times, &address);
  for(int i = 0; i<times ; i++){
    printf("0x%x HEX:0x%016lx DEC:%ld\n", address+i*4, paddr_read(address+i*4,4), paddr_read(address+i*4,4));
  }
  return 0;
}



static int cmd_info(char *args){
  char function;
  sscanf(args,"%c", &function);
  if(strcmp(args,"r") == 0)
    isa_reg_display();
  else
    print_wp();
  return 0;
}


static int cmd_p(char *args){
  word_t i;
  bool success;
  i = expr(args , &success);
  printf("%ld\n", i);
  return 0;
}

static int cmd_w(char *args){
  WP *n_wp=new_wp(args);
  printf("watchpoint %d:%s is set successfully.\n", n_wp->NO,n_wp->exp);
  return 0;
}

static int cmd_d(char *args){
  int num = 0;
  sscanf(args,"%d", &num);
  bool ans= delete_wp(num);
  if(ans){
    printf("Delete watchpoint %d successfilly!\n",num);
  }else{
    printf("There is no watchpoint whose NO. is %d.\n",num);
  }
  return 0;
}

static struct {
  const char *name;
  const char *description;
  int (*handler) (char *);
} cmd_table [] = {
  { "help", "Display informations about all supported commands", cmd_help },
  { "c", "Continue the execution of the program", cmd_c },
  { "q", "Exit NEMU", cmd_q },
  { "si", "Single Step", cmd_si},
  { "info", "print reg state", cmd_info},
  { "x", "scan mem", cmd_x},
  { "p", "expr", cmd_p},
  { "w", "watchpoint add", cmd_w},
  { "d", "watchpoint free", cmd_d}

  /* TODO: Add more commands */

};

#define NR_CMD ARRLEN(cmd_table)

static int cmd_help(char *args) {
  /* extract the first argument */
  char *arg = strtok(NULL, " ");
  int i;

  if (arg == NULL) {
    /* no argument given */
    for (i = 0; i < NR_CMD; i ++) {
      printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
    }
  }
  else {
    for (i = 0; i < NR_CMD; i ++) {
      if (strcmp(arg, cmd_table[i].name) == 0) {
        printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
        return 0;
      }
    }
    printf("Unknown command '%s'\n", arg);
  }
  return 0;
}

void sdb_set_batch_mode() {
  is_batch_mode = true;
}

void sdb_mainloop() {
  if (is_batch_mode) {
    cmd_c(NULL);
    return;
  }

  for (char *str; (str = rl_gets()) != NULL; ) {
    char *str_end = str + strlen(str);

    /* extract the first token as the command */
    char *cmd = strtok(str, " ");
    if (cmd == NULL) { continue; }

    /* treat the remaining string as the arguments,
     * which may need further parsing
     */
    char *args = cmd + strlen(cmd) + 1;
    if (args >= str_end) {
      args = NULL;
    }

#ifdef CONFIG_DEVICE
    extern void sdl_clear_event_queue();
    sdl_clear_event_queue();
#endif

    int i;
    for (i = 0; i < NR_CMD; i ++) {
      if (strcmp(cmd, cmd_table[i].name) == 0) {
        if (cmd_table[i].handler(args) < 0) { return; }
        break;
      }
    }

    if (i == NR_CMD) { printf("Unknown command '%s'\n", cmd); }
  }
}

void init_sdb() {
  /* Compile the regular expressions. */
  init_regex();

  /* Initialize the watchpoint pool. */
  init_wp_pool();
}
